<?php
/**
 * alltosun.com 门店列表统计
 * ============================================================================
 * 版权所有 (C) 2009-2011 北京互动阳光科技有限公司，并保留所有权利。
 * 网站地址: http://www.alltosun.com
 * ----------------------------------------------------------------------------
 * 许可声明：这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
 * ============================================================================
 * $Author: 高昂 (gaoa@alltosun.com) $
 * $Date: 2016-3-11 上午11:03:18 $
 * $Id: $
 */

class Action
{
    private $per_page = 20;
    
    /*
    * 显示列表
    */
    public function index()
    {
        $page = Request::getParam('page_no', 1);
        $status = Request::getParam('status', -1);
        $progress = Request::getParam('progress', '-1');
        $search_filter = Request::getParam('search_filter', array());
        $is_delete = Request::getParam('is_delete', 1);
        $test    = Request::getParam('test', 0);
        $is_execl = Request::getParam('is_execl', 0);
    
        // 拼装搜索条件
        $is_empty = 0;
        $filter = array();
        if (isset($search_filter['id']) && $search_filter['id']) {
            $filter['id'] = $search_filter['id'];
        }
    
        if (isset($search_filter['title']) && $search_filter['title']) {
            $filter['`title` LIKE '] = '%'.$search_filter['title'].'%';
        }
    
        if (isset($search_filter['start_date']) && $search_filter['start_date']) {
            $filter['add_time >='] = $search_filter['start_date'] . ' 00:00:00';
        }
        if (isset($search_filter['end_date']) && $search_filter['end_date']) {
            $filter['add_time <='] = $search_filter['end_date'] . ' 23:59:59';
        }
    
        if ($progress != '-1' || $status == 0) {
            $filter['status'] = $status;
        } else {
            $filter['1'] = 1;
        }
    
        if ('-1' != $status) {
            $filter['status'] = $status;
        } else {
            $filter['status >= '] = 0;
        }
    
        $agency_id_filter = array(0);
        // 传-1的时候查看所有的信息
        if ($is_delete != -1) {
            $filter['is_delete'] = $is_delete;
        }
    
        if ($test != 1) {
            $filter['is_enable'] = 1;
        }
    
        $store_list = array();
        $store_list = get_data_list('store', $filter, ' order BY `id` DESC ', $page, $this->per_page);
        if ($store_list) {
            $type_arr = store_config::$type_arr;
            foreach ($store_list as $k => $v) {
                $user_info = _uri('user', @$v['owner_id']);
                if ($user_info) {
                    $store_list[$k]['user_name'] = $user_info['user_name'];
                    $store_list[$k]['mobile']    = $user_info['mobile'];
                }
                if (isset($type_arr[$v['type']])) {
                    $store_list[$k]['type_name']  = $type_arr[$v['type']];
                }
                if ($v['agency_id']) {
                    $dealer_info = _uri('user', $v['agency_id']);
                    if ($dealer_info) {
                        $store_list[$k]['dealer_name'] = $dealer_info['realname'];
                        $store_list[$k]['dealer_mobile'] = $dealer_info['mobile'];
                    }
                }
                if (!$v['sign_user_id']) {
                    continue;
                }
                $sign_user_role_id = user_helper::get_user_role_id($v['sign_user_id']);
                $sign_user_info = user_helper::get_user_info($v['sign_user_id']);
                $store_list[$k]['sign_user_type'] = user_config::$role_arr[$sign_user_role_id];
                $store_list[$k]['sign_user_role_id'] = user_config::$role_arr[$sign_user_role_id];
                $store_list[$k]['sign_user_info'] = $sign_user_info;
                
                // 是否进货及进货额度
                $store_list[$k]['is_ever_stock'] = store_helper::get_store_stock_money($v['id'], 10);;
                $store_list[$k]['store_stock_money'] = store_helper::get_store_stock_money($v['id']);
                
            }
        }
        
        if ($is_execl) {
            $this->export_excel($store_list);
        }
    
        Response::assign('store_list', $store_list);
        Response::assign('status', $status);
        Response::assign('is_delete', $is_delete);
    
        Response::assign('progress', $progress);
        Response::assign('search_filter', $search_filter);
        Response::display('admin/store_data_list.html');
    }
    
    /**
     * 用户订单表
     */
    public function export_excel($list = array())
    {
        //初始化excel对象
        $objPHPExcel = new PHPExcel();
        $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
        //设置当前的sheet索引，用于后续的内容操纵
        $objPHPExcel->setActiveSheetIndex(0);
        $objActSheet = $objPHPExcel->getActiveSheet();
        // 哪几列
        $col_arr = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M');
        // 第一排的标题
        $col_title_arr = array('店铺id', '店铺头图', '店铺信息', '门店类型', '添加时间', '门店状态', '签约人(角色)', '是否进过货', '进货额度', '杯型信息(杯)', '订单类型', '订单状态', '支付方式');
        $col_content_arr = array('store_id', 'store_cover', 'store_info', 'store_type', 'add_time', 'sign_user', 'is_ever_stock', 'stock_money', 'price', 'store_info', 'cup_info', 'order_type', 'progress', 'pay_type');
        // 设置文档属性
        foreach ($col_arr as $k => $v) {
            //设置4个列的宽度
            if(in_array($v, array('A'))) {
                $objActSheet->getColumnDimension($v)->setWidth(8);
            } else {
                $objActSheet->getColumnDimension($v)->setWidth(20);
            }
            //设置单位格内容A1,B1,C1,D1
            $objActSheet->setCellValue($v."1", $col_title_arr[$k]);
            //格式化数字为字符串，避免导致数字默认右对齐单元格
            $objActSheet->getStyle($v)->getNumberFormat()->setFormatCode("@");
            //设置自动换行
            $objActSheet->getStyle($v)->getAlignment()->setWrapText(true);
        }
    
        // 填充单元格
        $key = 2;
        $mergeArr = array('A', 'B', 'G', 'H', 'I', 'J', 'K', 'L', 'M');
        foreach ($list as $k=>$v) {
            $mergeCells = 1;
//             if ($mergeCells > 1) {
//                 $objActSheet->mergeCells('A'.$key.':A'.($key+$mergeCells-1));
//                 $objActSheet->mergeCells('B'.$key.':B'.($key+$mergeCells-1));
//                 $objActSheet->mergeCells('G'.$key.':G'.($key+$mergeCells-1));
//                 $objActSheet->mergeCells('H'.$key.':H'.($key+$mergeCells-1));
//                 $objActSheet->mergeCells('I'.$key.':I'.($key+$mergeCells-1));
//                 $objActSheet->mergeCells('J'.$key.':J'.($key+$mergeCells-1));
//                 $objActSheet->mergeCells('K'.$key.':K'.($key+$mergeCells-1));
//                 $objActSheet->mergeCells('L'.$key.':L'.($key+$mergeCells-1));
//                 $objActSheet->mergeCells('M'.$key.':M'.($key+$mergeCells-1));
//             }
    
//             $order_type_list = order_config::$order_type;
//             $cup_name_list = cup_config::$cup_type;
//             $pay_type_list = order_config::$pay_type;
//             $order_progress = order_config::$dealer_progress;
            // 先将合并的信息填入excl
            foreach ($col_arr as $k2=>$v2) {
                $place = $v2.$key;
                $col_content = $col_content_arr[$k2];
                if (!in_array($v2, $mergeArr)) {
                    continue;
                }
    
                $value = '';
                if ($col_content == 'store_id') {
                    $value = $v['id'];
                } elseif ($col_content == 'store_info') {
                    $value = '阿迪发送到';
                    // 店铺名称：$v['title']\n 店长手机号：$v['mobile']\n经销商：$v['dealer_name']\n经销商手机号：$v['dealer_mobile']\n 店铺地址：$v['address']";
                } elseif ($col_content == 'store_type') {
                    //订单总商品数
                    $value = ' '.$v['type_name'];
                } elseif ($col_content == 'add_time') {
                    // 商家信息
                    $value = time_format($v['add_time']);
                } elseif ($col_content == 'sign_user') {
                    // 签约人(角色)
                    $value = $v['sign_user_info']['realname'].'('.$v['sign_user_type'].')';
                } elseif ($col_content == 'is_ever_stock') {
                    // 是否进过货
                    
                    if (isset($v['is_ever_stock']) && $v['is_ever_stock'] > 0) {
                        $value = '是';
                    } else {
                        $value = '否';
                    }
                } elseif ($col_content == 'stock_money') {
                    if (isset($v['store_stock_money'])) {
                        $value = $v['store_stock_money'];
                    } else {
                        $value = 0;
                    }
                   
                } elseif ($col_content == 'number') {
                    $value = $k+1;
                }
    
                $objActSheet->setCellValue($place, $value);
            }
//             foreach ($v['order_relation'] as $r_k => $order_relation_info) {
//                 $num = $key+$r_k;
//                 $goods_info = goods_helper::get_goods_info($order_relation_info['goods_id']);
//                 $goods_title   = $goods_info['title'];
//                 $goods_price   = $order_relation_info['goods_price'];
//                 $goods_num     = $order_relation_info['goods_num'];
//                 $real_price     = $order_relation_info['real_price'];
//                 $objActSheet->setCellValue('C'.$num, $goods_title);
//                 $objActSheet->setCellValue('D'.$num, ' '.$goods_price);
//                 $objActSheet->setCellValue('E'.$num, $real_price);
//                 $objActSheet->setCellValue('F'.$num, $goods_num);
//             }
    
            if ($mergeCells > 1) {
                $key += $mergeCells;
            } else {
                $key++;
            }
    
            usleep(1000);
        }
    
        //设置页面类型
        header("Content-Type: application/vnd.ms-excel");
        //用于下载的文件头
        header("Content-Type: application/force-download");
        header("Content-Type: application/download");
        header("Content-Type: application/octet-stream");
    
        //$title = trim($catebirthday_gift_recordry_name."表").".xls";
        $title = date('Y-m-d')."订单信息.xls";
        // ff正常，ie不正常，下面是从网站找到的解决方案
        if (preg_match('/MSIE/',$_SERVER['HTTP_USER_AGENT'])) {
            $title = rawurlencode($title);
        }
        header("Content-Disposition: attachment; filename=".$title);
        header("Content-Transfer-Encoding: binary");
        //最后修改时间
        header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
        //不使用缓存
        header('Cache-Control: ust-revalidate, post-check=0, pre-check=0');
        header('Cache-Control: public');
        header("Expires: 0");
        header("Pragma: public");
        $objWriter->save('php://output');
        exit;
    }
    
}


?>